home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / library / hack / cprogramming.txt < prev    next >
Encoding:
Text File  |  1998-07-17  |  10.0 KB  |  345 lines

  1. C programming for the complete newbie
  2.  
  3.         Hello there im Krisis you may have seen me on irc.hackersclub.com. 
  4. Well I thought it was about time to write an article like everyone else. But
  5. unlike many others 
  6. mine wont be on Hacking, Cracking, or Phreaking it's on C programming, you
  7. see I'm not the best 
  8. hacker but I'm an ok programmer. So here it goes. This is based for absolute
  9. beginners so those 
  10. of you  like my friend Chrak wouldn't be interested in it.
  11.  
  12.         1st lets talk about some of C 's history. C was invent by Dennis Ritchie
  13. and Kenneth
  14.  Thompson. They modeled it after the language they were using called B. C
  15. was a subset of B hence
  16. the name. C was made because B was going out of style and they needed a new
  17. language to write 
  18. UNIX in. Yes UNIX was made in C. C was made popular very quickly because
  19. every UNIX sold had a C
  20. compiler. A compiler is a program thats looks at your source code and
  21. transfers it into object 
  22. code, after it is transfered into object code it must be linked, once it's
  23. linked it can be
  24. executed.
  25.  
  26. /***************************************************************************
  27. ********************/
  28.         2nd Lets talk about variables they are your integers and characters and so
  29. on. You have 
  30. many data types they are.
  31.                                 int  integers
  32.                                 
  33.                                 char characters
  34.                                 
  35.                                 long int bigger integers
  36.                                 
  37.                                 short int same as int
  38.  
  39.                                 float decimal numbers
  40.  
  41.                                 double even bigger decimal numbers
  42.  
  43. To define a variable 1st you must put something like   int MyNum;
  44. MyNum = 2;
  45. or for a character value it would be like              char name;       name
  46. = 'Jim';
  47.  
  48. notice the ''  you must have those around character values, they are not
  49. needed for integers or
  50. decimals.
  51.  
  52. /***************************************************************************
  53. ********************/
  54.         3rd I'll tell you about stuff like #include and #define. #include is used
  55. to tell the 
  56. compiler that whatever is in the brackets just be included like its part of
  57. your code. 
  58.  
  59. #include <stdio.h> 
  60.  
  61. #define is used to define something Like the color of a truck or car.
  62.  
  63. #define TRUCK "red"
  64.         
  65. #include and #define must come before any functions are even prototyped
  66. (I'll talk about this 
  67. later). 
  68.  
  69. /***************************************************************************
  70. ********************/   
  71.         4th I'll talk about functions. Every program must have at least one
  72. function. That 
  73. functions name must be main(). The () tells the compiler that it is a
  74. function. All functions 
  75. must return a value in the main() function a 0 is usually returned. In your
  76. functions you will
  77. want to use comment's to explain your code a comment is begun by using /*
  78. and ended by using */ .
  79. I will now show you your 1st program.
  80.  
  81.  
  82. #include <stdio.h>      /* Used in most standard Input Output Programs */
  83. main()
  84. {                       /* Beginning Brackets used to show the beggining of
  85. a block of code */
  86. printf("Hello World");  /* A function already written in Stdio.h */
  87. return 0;               /* Value returned from the program */
  88. }                       /* Ending bracket used to show end of a block of code */
  89.  
  90. now compile your program in your compiler if your using UNIX do it like this 
  91. gcc hello.c -o Hello
  92. and then run your program bye typing in ./Hello
  93.  
  94. /***************************************************************************
  95. ********************/
  96.         5th I'll talk about output which is essential to almost all programs. 
  97. I'll start you out with printf(); It is defined in Stdio.h so every time you
  98. call printf(); you 
  99. must include Stdio.h .
  100. printf(); 's syntax is quite easy you just used it like this
  101.  
  102. printf("What ever you want outputted");
  103.  
  104. to output variables you do it like this 
  105.  
  106. char dog='scruffy';
  107. printf("My dogs name is %c", dog);  notice the %c it tells the compiler to
  108. look for a character 
  109.                                     variable.
  110.  
  111. Now for Integers and Decimals
  112.  
  113. int age=16;
  114. printf("I am %d year's old", age);  use %d to print out decimals and integers 
  115.  
  116. /***************************************************************************
  117. ********************/
  118. 6th Lets talk about multiple functions. When you have more than one function
  119. you must prototype 
  120. it. Here is an example.
  121.  
  122. #include <stdio.h>
  123. void hello(); /* This is a prototype notice the void. Void tells the
  124. compiler that this function
  125.                  does not return a value like return 0; */
  126. main() /* Main doesnt ever need to be prototyped */
  127. {  
  128. hello();
  129. return 0;
  130. }
  131.  
  132. void hello(); /* Your prototype must look exactly like your real function */
  133. {
  134. printf("Im in the function hello!");
  135.  
  136. Void is your return type. Other return types are int for returning integers
  137. use float to return 
  138. decimals and so on.
  139.  
  140. /***************************************************************************
  141. ********************/
  142. 7th I'll introduce you to input. Ill teach you how to use gets() and scanf()
  143. and fgets() properly
  144. gets() takes a variable and place data into as do scanf() and fgets() In the
  145. next example I will 
  146. use all 3
  147.  
  148. #include <stdio.h>
  149. #include <conio.h>
  150.  
  151. main()
  152. {
  153. int x, y, z, ans;
  154.  
  155. printf("What is X 's value \n ");
  156. gets(x);
  157. printf("What is Y 's value \n");
  158. scanf("%d", &y); /* Scanf is odd I dont recommend using it try and use gets
  159. and fgets more */ 
  160. /* Whatever is used to print the variable type you are using is placed in
  161. parantheses and & is 
  162.    used in front of whatever variable you are using */
  163.  
  164. printf("What is Z 's value");
  165. fgets(z, 25, stdin); /* fgets is kinda tricky at first glance */ 
  166.  
  167. /* first off you put what variable you want then how many integers  or
  168. characters
  169. long it can be and then stdin,stdin is a macro defined in stdio.h it is used
  170. to represent 
  171. standard input */
  172.  
  173. ans=x+y+z;
  174. printf("Ans equals %d", ans);
  175.  
  176. /***************************************************************************
  177. ********************/
  178. 8th Lets talk about decision statements like if and else.
  179. here is how if is used
  180.  
  181. if(VariableName==5)                          
  182.   {
  183.    printf("Your variable is 5"); 
  184.   }
  185.  
  186. else is used after if, it is used like this
  187.  
  188. if(VariableName==5)
  189.   {
  190.    printf("Your variable is 5");
  191.   }
  192. else
  193.   {
  194.    printf("I dont know what your variable is");
  195.   }
  196.  
  197. /***************************************************************************
  198. ********************/
  199. 9th Ill talk about While loops and do-while loops. Loops aren't as hard as
  200. they may seem.
  201. while loops are easy. Just watch and learn.
  202.  
  203. #include <stdio.h>
  204. main()
  205. {
  206. int x=1;
  207.         while(x<2600)
  208.         {
  209.         printf("X=%d",x);
  210.         x++; /* adds 1 to x */
  211.         }
  212. return 0;
  213. } /* While loops dont have to happen only if the right sequence happens do
  214. they execute */
  215.  
  216. Loops can be placed inside of IF and else statements if you want. That can
  217. be very helpful if you 
  218. want a process to happen a bunch if something happens Like the user pressing
  219. X instead of Y.
  220.  
  221. Do-While loops are just as easy. They automatically execute at least once.
  222.  
  223. #include <stdio.h>
  224. main()
  225. {
  226. int x=1;
  227.         do
  228.         {
  229.         printf("X=%d",x);
  230.         x++;
  231.         }while(x<2600);
  232. return 0;
  233. }
  234.  
  235. The do tells the program to do this at least once and it doesn't see the
  236. while until it has 
  237. already do the do.
  238.  
  239. /***************************************************************************
  240. ********************/
  241. 10th I'm going to tell you about another kind of loop the for loop. For
  242. loops execute a given 
  243. number of times and then stop. For loops are executed like this.
  244.  
  245. for(x=1; x<100; x++)
  246. {
  247. printf("X=%d",x);
  248. }
  249. That prints 1 through 100. Thats about it about for loops there not very
  250. hard. They can be pretty
  251. useful. But I dont use them alot I'm into While loops.
  252.  
  253. /***************************************************************************
  254. ********************/
  255. 11th is all about Arrays. Arrays are consecutive places in memory. Arrays
  256. can be integers and 
  257. characters. They can be just about any size. Here is an example.
  258.  
  259. #include <stdio.h>
  260. main()
  261. {
  262. int i[2]
  263.  
  264. int i[1] =2600
  265. int i[2] =1982 /* Year I was born */
  266.  
  267. printf(" I[1] = %d ",i[1]); 
  268. printf(" I[2] = %d ",i[2]);
  269.  
  270. return 0;
  271. }
  272.  
  273. See how easy that was Arrays aren't very hard at all.
  274.  
  275. /***************************************************************************
  276. ********************/
  277.  
  278. 12th I'll tell you about passing parameters to functions. Its nots to hard
  279. but I've said that 
  280. about everything. First you must prototype it before main() I hope you
  281. remember how to prototype.
  282. Here is an example of passing parameters.
  283.  
  284. #include <stdio.h>
  285. int next(int x);
  286.  
  287. main()
  288. {
  289. int age;
  290. printf("please enter you age ");
  291. fgets(age, 3 ,stdin);
  292. age(age); /* Age is passed on to the next function */
  293.  
  294. return 0;
  295. }
  296.  
  297. next(int x);
  298. {
  299. x++;
  300. printf("Next year you will be %d", x);
  301. return 0;
  302. }
  303.  
  304. /***************************************************************************
  305. ********************/
  306. 13th I'll say a little sumthin about why C is good to Hacking. It's good
  307. because it is so 
  308. portable C can be used on all processors and Operating Systems. So if your
  309. exploit you just wrote works on
  310. one UNIX like OS odds are it will work on another, therefore you dont have
  311. to write a whole new
  312. program just to get a root shell.
  313.  
  314. /***************************************************************************
  315. ********************/
  316. Last but not least a sample program.
  317.  
  318. #include <stdio.h>
  319. int blah(int x,int y);
  320. main()
  321. {
  322. int a,b,r;
  323. printf("Enter some numbers ");
  324. scanf("%d", &a);
  325. scanf("%d", &b);
  326. r=blah(a,b);
  327. printf("R = %d",r);
  328. return 0;
  329. }
  330.  
  331. int blah(int x, int y)
  332. { return x * y; }
  333.  
  334. What does blah do and how does it work?    You tell me.
  335. /***************************************************************************
  336. ********************/
  337. Thats it for my little tutorial on C. I hope it helped you some. But for
  338. further info on C I 
  339. suggest reading 
  340. C programming in 12 easy lessons by Greg Perry from Sams Publishing.
  341. It helped my a shitload on learning C. You also might want to get some books
  342. on C++ a subset of C
  343.  
  344. You can mail me at reid@programmerz.org